/**
 * 验证日期格式
 */
function date(value) {
	return !/Invalid|NaN/.test(new Date(value).toString())
}

/**
 * 格式化当前时间
 */
function getDateTime(format = '-', showTime = false) {
	let date = new Date(),
		year = date.getFullYear(),
		month = date.getMonth() + 1,
		day = date.getDate(),
		hour = date.getHours() + 1,
		minute = date.getMinutes(),
		second = date.getSeconds();
	month = checkTime(month);
	day = checkTime(day);
	hour = checkTime(hour);
	minute = checkTime(minute);
	second = checkTime(second);

	function checkTime(i) {
		if (i < 10) {
			i = "0" + i;
		}
		return i;
	}
	if (showTime) {
		return year + format + month + format + day + " " + hour + ":" + minute + ":" + second
	} else {
		return year + format + month + format + day
	}
}

/**
 * 计算两个时间的时间差,返回天数
 */
function getTwoDateDiffer(date1, date2) {
	let aDate, oDate1, oDate2, deffDays;
	aDate = date1.split("-");
	oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为yyyy-MM-dd格式
	aDate = date2.split("-");
	oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
	deffDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
	return deffDays; //返回相差天数
}
/**
 * 计算当前时间的时间差,返回秒数
 */
function getTwoDateDifferSecond(date1) {
	let aDate, oDate1, oDate2, deffSecond;
	aDate = date1.split("-");
	oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为yyyy-MM-dd格式
	// aDate = date2.split("-");
	// oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
	var date = new Date();
	oDate2 = date.getTime();
	console.log(Math.abs(oDate1),oDate2);
	deffSecond = parseInt(Math.abs(oDate1 - oDate2) / 1000); //把相差的毫秒数转换为秒数
	return deffSecond; //返回相差秒数
}
export default {
	date,
	getDateTime,
	getTwoDateDiffer,
	getTwoDateDifferSecond
}
